// Functions associated with the Timer class
//-----------------

//-----------------
// Includes libraries and other files
#include "Timer.h"
//-----------------

//-----------------
//// CONSTRUCTOR - Timer
Timer::Timer()
	: start_time_( 0), time_in_microseconds_( 0)
{}

//// DESTRUCTOR - Timer
Timer::~Timer() {}
//-----------------

//-----------------
//// FUNCTION - get_time_current
// return the time during a recording session (i.e. in-game)
std::ostringstream Timer::get_time_current() const {
	// Pre-conditions :- none
	assert( true);

	unsigned int t( 0);

	if( start_time_ != 0)
		t = time_in_microseconds_ + (timeGetTime() - start_time_);
	else
		t = time_in_microseconds_;

	return get_time_formatted( t);
}

//// FUNCTION - get_time_end
// return the time recorded thus far
std::ostringstream Timer::get_time_end() const {
	// Pre-conditions :- none
	assert( true);

	return get_time_formatted( time_in_microseconds_);
}

//// FUNCTION - get_time_formatted
// return the time in minutes and seconds as a stringstream
std::ostringstream Timer::get_time_formatted( const unsigned int& s) const {
	// Pre-conditions :- none
	assert( true);

	std::ostringstream os;

	if(( s/1000)/60 < 10)
		os << "0";
	os << (( s/1000)/60) << ":";

	if(( s/1000)%60 < 10)
		os << "0";
	os << (( s/1000)%60);

	return os;
}

//// FUNCTION - get_time_in_seconds
// return the time recorded, in seconds
unsigned int Timer::get_time_in_seconds() const {
	// Pre-conditions :- none
	assert( true);

	return( time_in_microseconds_/1000);
}

//// FUNCTION - timer_start_new
// record the starting time and reset previous values
void Timer::timer_start_new() {
	// Pre-conditions :- none
	assert( true);

	time_in_microseconds_ = 0;
	start_time_ = timeGetTime();
}

//// FUNCTION - timer_start
// record the starting time
void Timer::timer_start() {
	// Pre-conditions :- none
	assert( true);

	start_time_ = timeGetTime();
}

//// FUNCTION - time_stop
// stop the timer and record the difference in time
void Timer::timer_stop() {
	// Pre-conditions :- the timer must've been started beforehand
	assert( start_time_ != 0);

	int end( timeGetTime());

	// the time taken is the current time minus the time that the timer was started
	time_in_microseconds_ += (end - start_time_);

	// reset the variable in case this function is called twice in succession; thus triggering the assert
	start_time_ = 0;
}